﻿using System;
using System.Collections.Generic;
using System.Text;
using CLoggerNamespace;

namespace HL7v_2x
{
    public class HL7SegmentEVN :  IHL7Segment
    {
        #region DESARMAR SEGMENTO EN CAMPOS
        public List<HL7Fields> DesarmarSegmentoEnCampos(string strSegment)
          {
              //EJEMPLO MENSAJE HL7
              /*
                EVN|A01|20050426113700||||20050426113700|<cr>
              */

             List<HL7Fields> lstHL7Fields = new List<HL7Fields>();

              string[] strArrAux = null;
              try 
              {

                  strArrAux = strSegment.Split('|');

                  int inCantBarras = strArrAux.Length - 1;

                  CLogger.Append("Cantidad de Barras: " + inCantBarras  + " - MAXIMO DE 7 CAMPOS.", CLogger.INFO);

              }catch(Exception ex)
              {
                  CLogger.Append("ERROR PARCEANDO LAS BARRAS | Ex: " + ex.Message, CLogger.INFO);
                  throw ex;
              }


              try
              {
                  if ((strArrAux.Length > 0) && (strArrAux[0] != null) && (strArrAux[0] != ""))    // TypecodeEvent_EVN = 00099,
                  {
                      CLogger.Append("Field 0: " + strArrAux[0], CLogger.INFO);
                      System.Text.ASCIIEncoding enc = new ASCIIEncoding();
                      lstHL7Fields.Add(new HL7Fields(HL7FieldName.TypecodeEvent_EVN, enc.GetBytes(strArrAux[0])));
                  }
                  if ((strArrAux.Length > 1) && (strArrAux[1] != null) && (strArrAux[1] != null))  // RecordedDateTime_EVN = 00100,
                  {
                      CLogger.Append("Field 1: " + strArrAux[1], CLogger.INFO);
                      System.Text.ASCIIEncoding enc = new ASCIIEncoding();
                      lstHL7Fields.Add(new HL7Fields(HL7FieldName.RecordedDateTime_EVN, enc.GetBytes(strArrAux[1])));
                  }
                  if ((strArrAux.Length > 2) && (strArrAux[2] != "") && (strArrAux[2] != null)) //  DateTimePlannedEvent_EVN = 00101,
                  {
                      CLogger.Append("Field 2: " + strArrAux[2], CLogger.INFO);
                      System.Text.ASCIIEncoding enc = new ASCIIEncoding();
                      lstHL7Fields.Add(new HL7Fields(HL7FieldName.DateTimePlannedEvent_EVN, enc.GetBytes(strArrAux[2])));
                  }
                  if ((strArrAux.Length > 3) && (strArrAux[3] != "") && (strArrAux[3] != null)) //  EventReasonCode_EVN = 00102, 
                  {
                      CLogger.Append("Field 3: " + strArrAux[3], CLogger.INFO);
                      System.Text.ASCIIEncoding enc = new ASCIIEncoding();
                      lstHL7Fields.Add(new HL7Fields(HL7FieldName.EventReasonCode_EVN, enc.GetBytes(strArrAux[3])));
                  }
                  if ((strArrAux.Length > 4) && (strArrAux[4] != "") && (strArrAux[4] != null)) //  OperatorID_EVN = 00103,
                  {
                      CLogger.Append("Field 4: " + strArrAux[4], CLogger.INFO);
                      System.Text.ASCIIEncoding enc = new ASCIIEncoding();
                      lstHL7Fields.Add(new HL7Fields(HL7FieldName.OperatorID_EVN, enc.GetBytes(strArrAux[4])));
                  }
                  if ((strArrAux.Length > 5) && (strArrAux[5] != "") && (strArrAux[5] != null)) //  EventOcurred_EVN = 01278,
                  {
                      CLogger.Append("Field 5: " + strArrAux[5], CLogger.INFO);
                      System.Text.ASCIIEncoding enc = new ASCIIEncoding();
                      lstHL7Fields.Add(new HL7Fields(HL7FieldName.EventOcurred_EVN, enc.GetBytes(strArrAux[5])));
                  }
                  if ((strArrAux.Length > 6) && (strArrAux[6] != "") && (strArrAux[6] != null)) //EventFacility_EVN = 01534,
                  {
                      CLogger.Append("Field 6: " + strArrAux[6], CLogger.INFO);
                      System.Text.ASCIIEncoding enc = new ASCIIEncoding();
                      lstHL7Fields.Add(new HL7Fields(HL7FieldName.EventFacility_EVN, enc.GetBytes(strArrAux[6])));
                  }
              }
              catch (Exception ex)
              {                                    
                  throw new Exception("EXCEPCION AL DESCOMPONER SEGMENTO EVN EX:"+ex.Message);
              }

            CLogger.Append("**********************FINAL DEL DESCOMPONER SEGMENTO EVN OK*******************", CLogger.INFO);

           

              return lstHL7Fields;

          }
        #endregion
        

        //public HL7SegmentEVN()
        //    : base(HL7MessageName.MessageEVN)
        //{

        //}

        //public int BuildFields(byte[] byteInputStreamParam)
        //{
        //    throw new NotImplementedException();
        //}

        //public int BuildFields(byte[] byteInputStreamParam, int intOffset)
        //{
        //    throw new NotImplementedException();
        //}
    }
}
